package edu.software.PBL_Management_System.comment.service;

import edu.software.PBL_Management_System.comment.repository.CommentRepository;
import edu.software.PBL_Management_System.demand.repository.DemandRepository;
import edu.software.PBL_Management_System.entity.Comment;
import edu.software.PBL_Management_System.entity.Demand;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class CommentService {

    //依赖注入
    @Resource
    private CommentRepository commentRepository;

    @Resource
    private DemandRepository demandRepository;

    // 创建新评论
    @Transactional
    public Comment createComment(Comment comment) {
        return commentRepository.save(comment);
    }

    // 回复评论
    @Transactional
    public Comment replyToComment(Long parentId, Comment reply) {
        Optional<Comment> parentComment = commentRepository.findById(parentId);
        if (parentComment.isPresent()) {
            reply.setParentId(parentId);
            return commentRepository.save(reply);
        }
        throw new RuntimeException("Parent comment not found");
    }

    // 获取需求的所有评论
    public List<Comment> getDemandComments(Long demandId) {
        return commentRepository.findByDemandIdOrderByCreatedTimeDesc(demandId);
    }

    // 获取评论的所有回复
    public List<Comment> getCommentReplies(Long commentId) {
        return commentRepository.findByParentIdOrderByCreatedTimeDesc(commentId);
    }

    // 获取用户收到的评论
    public List<Comment> getUserReceivedComments(List<Long> userDemandIds) {
        return commentRepository.findByDemandIdInOrderByCreatedTimeDesc(userDemandIds);
    }

    // 标记评论为已读
    @Transactional
    public void markCommentAsRead(Long commentId) {
        Optional<Comment> comment = commentRepository.findById(commentId);
        comment.ifPresent(c -> {
            c.setIsRead(true);
            commentRepository.save(c);
        });
    }

    // 获取用户未读评论数量
    public Long getUnreadCommentCount(Long userId) {
        return commentRepository.countByUserIdAndIsReadFalse(userId);
    }

    // 获取需求评论数量
    public Long getDemandCommentCount(Long demandId) {
        return commentRepository.countByDemandId(demandId);
    }

    // 删除评论
    @Transactional
    public void deleteComment(Long commentId) {
        commentRepository.deleteById(commentId);
    }


    // 获取需求未读评论数量
    public Long getDemandUnreadCount(Long demandId) {
        return commentRepository.countByDemandIdAndIsReadFalse(demandId);
    }

    // 获取需求最新消息时间
    public Map<Long, String> getDemandsLatestMessageTime(List<Long> demandIds) {
        List<Comment> latestComments = commentRepository.findLatestCommentsByDemandIds(demandIds);
        return latestComments.stream()
                .collect(Collectors.toMap(
                        Comment::getDemandId,
                        comment -> comment.getCreatedTime().toString(),
                        (existing, replacement) -> existing
                ));
    }

    // 获取专家评论过的需求列表
    public List<Demand> getExpertCommentedDemands(Long userId) {
        // 获取专家评论过的所有需求ID
        List<Long> demandIds = commentRepository.findDistinctDemandIdsByUserId(userId);
        // 根据需求ID获取需求详情
        return demandRepository.findAllById(demandIds);
    }
}
